package com.gxkj.projects.myshopx.services.impl;

import com.gxkj.common.exceptions.BusinessException;
import com.gxkj.common.exceptions.ValidateException;
import com.gxkj.common.utils.ListPager;
import com.gxkj.common.utils.ValidatorUtil;
import com.gxkj.projects.myshopx.dao.DbConfigDaoImpl;
import com.gxkj.projects.myshopx.entitys.DbConfig;
import com.gxkj.projects.myshopx.entitys.User;
import com.gxkj.projects.myshopx.enums.DbConfigTypeEnum;
import com.gxkj.projects.myshopx.enums.ErrorCodeEnum;
import com.gxkj.projects.myshopx.services.DbConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DbConfigServiceImpl implements DbConfigService {

    Logger LOG = LoggerFactory.getLogger(DbConfigServiceImpl.class);

    @Autowired
    private DbConfigDaoImpl dbConfigDao;

    @Autowired
    private ValidatorUtil validatorUtil;

    /**
     * 用户不允许增加系统配置
     * @param user
     * @param entity
     * @throws ValidateException
     */
    public void doAddDbConfig(User user, DbConfig entity) throws ValidateException {
         entity.setProType(DbConfigTypeEnum.USRER);
        validatorUtil.validate(entity,true);

        if(dbConfigDao.isExitByName(entity.getId(),entity.getProName())){
            BusinessException businessException = new BusinessException(ErrorCodeEnum.BUSSINESS_ERROR.getCode(),"该名称已经存在");
            throw businessException;
        }else {
            dbConfigDao.insert(entity);
        }


    }


    /**
     * 类型和Id与数据库中的一致
     * 如果是系统类型的属性名称不允许更改
     * @param user
     * @param entity
     * @throws ValidateException
     */
    public void doUpdateDbConfig(User user, DbConfig entity) throws ValidateException {
        DbConfig dbentity = this.getById(entity.getId());
        entity.setProType(dbentity.getProType());
        if(entity.getProType().equals(DbConfigTypeEnum.SYSTEM)){
            entity.setProName(dbentity.getProName());
        }
        validatorUtil.validate(entity,true);
        //验证属性名称,防止重复
        if(dbConfigDao.isExitByName(entity.getId(),entity.getProName())){
            BusinessException businessException = new BusinessException(ErrorCodeEnum.BUSSINESS_ERROR.getCode(),"该名称已经存在");
            throw businessException;
        }else {
            dbConfigDao.updateByMerge(entity);
        }
    }

    /**
     * 系统类型的属性禁止删除
     * @param user
     * @param id
     */
    public void doDeleteDbConfig(User user, String id) {
        DbConfig dbentity = this.getById(id);
        if(dbentity != null && dbentity.getProType() == DbConfigTypeEnum.SYSTEM){
            //禁止删除
            BusinessException businessException = new BusinessException(ErrorCodeEnum.BUSSINESS_ERROR.getCode(),"系统属性禁止删除");
            throw businessException;
        }else {
            dbConfigDao.deleteById(id,DbConfig.class);
        }
    }


    public ListPager<DbConfig> doPageHQL(int pagenNo, int pageSize, DbConfig entity) {
        return dbConfigDao.doPageHQL(pagenNo,pageSize,entity);
    }


    public DbConfig getById(String id) {
        DbConfig dbentity = dbConfigDao.selectById(id,DbConfig.class);
        return dbentity;
    }
}
